package com.bd.scm.module.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bd.offices.excel.ExcelUtil;
import com.bd.scm.dao.mapper.user.UserVacationMapper;
import com.bd.scm.domain.dto.system.BdProcResetDataDto;
import com.bd.scm.domain.models.user.UserVacation;
import com.bd.scm.domain.models.user.UserVacationImport;
import com.bd.scm.domain.models.user.UserVacationImport2;
import com.bd.scm.domain.models.user.UserVacationImport3;
import com.bd.scm.module.enums.ExceptionEmun;
import com.bd.scm.module.exception.ScmException;
import com.bd.scm.module.user.service.UserVacationService;
import com.bd.scm.module.utils.ResultUtil;
import com.bd.utils.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class UserVacationServiceImpl implements UserVacationService {
    @Resource
    private UserVacationMapper userVacationMapper;

    @Override
    public List<UserVacation> vacationList(UserVacation userVacation) {
        return userVacationMapper.vacationList(userVacation);
    }

    @Override
    public int updateJqInfo(UserVacation userVacation, String clientIp) {
        return userVacationMapper.updateJqInfo(userVacation);
    }

    @Override
    @Transactional
    public int sockVacation(UserVacation userVacation, String clientIp) {
        int n = 0;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        JSONObject user = (JSONObject) request.getSession().getAttribute("user");
        int operator = user.getInteger("userSerial");
        String userSerial = userVacation.getUserSerial();
        String[] split = userSerial.split(",");
        UserVacation u = new UserVacation();
        u.setOperator(operator);
        for (String s : split) {
            u.setUserSerial(s);
            int faDing = userVacationMapper.getFaDing(u);
            if(Integer.valueOf(userVacation.getSdjqts())<0){
                int sdjqts = userVacationMapper.getSdjqts(u);
                if(Integer.parseInt(userVacation.getSdjqts())*-1>sdjqts){
                    throw new ScmException(ExceptionEmun.USER_ERROR_FADING3);
                }
                faDing = Integer.parseInt(userVacation.getSdjqts())+1;
            }
            if(faDing==0){
                throw new ScmException(ExceptionEmun.USER_ERROR_FADING);
            }else if(Integer.parseInt(userVacation.getSdjqts())>faDing){
                throw new ScmException(ExceptionEmun.USER_ERROR_FADING2);
            }else{
//                userVacationMapper.reduceNianJia(u);
//                n+=userVacationMapper.sockVacation(u);
//                userVacationMapper.addholidayInfo3(u);
//                userVacationMapper.addholidayInfo7(u);

                u.setSxsj(userVacation.getSdsxsj());
                u.setJqsc(Integer.valueOf(userVacation.getSdjqts()));
                u.setLx(3);
                n+=userVacationMapper.luRuVacation(u);
//                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//                df.format(new Date());
//                String a = u.getSxsj();
                userVacationMapper.execTaskRuVacation();
//                if(u.getSxsj()==u.getSxsj()){
//                userVacationMapper.execluRuVacation(u);
//                }

            }

        }
        return n;
    }

    @Override
    public int setLimit(UserVacation userVacation, String clientIp) {
        int n = 0;
        String userSerial = userVacation.getUserSerial();
        String[] split = userSerial.split(",");
        UserVacation u = new UserVacation();
        for (String s : split) {
            u.setUserSerial(s);
            u.setTxsx(userVacation.getTxsx());
            n+=userVacationMapper.setLimit(u);
        }
        return n;
    }

    @Override
    @Transactional
    public int clearingZero(UserVacation userVacation, String clientIp) {
        int n = 0;
        String userSerial = userVacation.getUserSerial();
        String[] split = userSerial.split(",");
        UserVacation u = new UserVacation();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        JSONObject user = (JSONObject) request.getSession().getAttribute("user");
        int operator = user.getInteger("userSerial");
        String currentDate = DateUtils.getCurrentDate();
        u.setOperator(operator);
        for (String s : split) {
            u.setUserSerial(s);
            //保存消费时段与班次
            n+=this.userVacationMapper.addholidayInfo(u);
            //重新计算假期
            BdProcResetDataDto bdProcResetDataDto = new BdProcResetDataDto();
            bdProcResetDataDto.setBeginDate(currentDate);
            bdProcResetDataDto.setLx(2);
            bdProcResetDataDto.setUserSerial(s);
            bdProcResetDataDto.setOperator(operator+"");
            this.userVacationMapper.execResetData(bdProcResetDataDto);

//            this.userVacationMapper.clearingZero(u);
        }
        return n;
    }

    @Override
    @Transactional
    public int flClearingZero(UserVacation userVacation, String clientIp) {
        int n = 0;
        String userSerial = userVacation.getUserSerial();
        String[] split = userSerial.split(",");
        UserVacation u = new UserVacation();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        JSONObject user = (JSONObject) request.getSession().getAttribute("user");
        int operator = user.getInteger("userSerial");
        u.setOperator(operator);
        for (String s : split) {
            u.setUserSerial(s);
            //保存消费时段与班次
            this.userVacationMapper.addholidayInfo2(u);
            n+=this.userVacationMapper.flClearingZero(u);
        }
        return n;
    }

    @Override
    @Transactional
    public int luRuVacation(UserVacation userVacation, String clientIp) {
        int n = 0;
        String userSerial = userVacation.getUserSerial();
        String[] split = userSerial.split(",");
        UserVacation u = new UserVacation();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        JSONObject user = (JSONObject) request.getSession().getAttribute("user");
        int operator = user.getInteger("userSerial");
        u.setOperator(operator);
        for (String s : split) {
            if(2==userVacation.getLx()){
                //判断调休不超过调休上限
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("userSerial",s);
                jsonObject.put("minute",userVacation.getJqsc());
                int ifRestrict = this.userVacationMapper.getBdNianjiaOne(jsonObject);
                if(ifRestrict!=0){
                    throw new ScmException(ExceptionEmun.USER_ERROR_FADING4);
                }
            }
            u.setUserSerial(s);
            u.setLx(userVacation.getLx());
            u.setSxsj(userVacation.getSxsj());
            u.setJqsc(userVacation.getJqsc());
            //保存消费时段与班次
            n+=userVacationMapper.luRuVacation(u);
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//            df.format(new Date());
//            String a = userVacation.getSxsj();
            userVacationMapper.execTaskRuVacation();
        }
        return n;
    }


    @Override
    public void execTaskRuVacation() {
        userVacationMapper.execTaskRuVacation();
    }

    @Override
    @Transactional
    public String upJiaQiExcel(MultipartFile file, HttpServletRequest request) throws Exception  {
        String r = null;
        InputStream inputStream = new BufferedInputStream(file.getInputStream());
        List<UserVacationImport> list = ExcelUtil.ImportExcel(inputStream, UserVacationImport.class);
        JSONObject user = (JSONObject) request.getSession().getAttribute("user");
        int operator = user.getInteger("userSerial");
        String currentDate = DateUtils.getCurrentDate();
        for(UserVacationImport userVacationImport : list){
            String userSerial = userVacationMapper.isExistByUserNo(userVacationImport.getUserNo());
            if(!StringUtils.isEmpty(userSerial)){
                userVacationImport.setOperator(operator);
                userVacationImport.setUserSerial(userSerial);
//                userVacationMapper.updateNianJia(userVacationImport);
                userVacationMapper.addholidayInfo4(userVacationImport);

                //重新计算假期
                BdProcResetDataDto bdProcResetDataDto = new BdProcResetDataDto();
                bdProcResetDataDto.setBeginDate(currentDate);
                bdProcResetDataDto.setLx(2);
                bdProcResetDataDto.setUserSerial(userSerial);
                bdProcResetDataDto.setOperator(operator+"");
                this.userVacationMapper.execResetData(bdProcResetDataDto);

            }else{
                throw new ScmException(ExceptionEmun.USER_ERROR_NOTFOUND);
            }
        }
        return null;
    }

    @Override
    @Transactional
    public String upLockVacationExcel(MultipartFile file, HttpServletRequest request) throws Exception {
        String r = null;
        JSONObject user = (JSONObject) request.getSession().getAttribute("user");
        int operator = user.getInteger("userSerial");
        UserVacation u = new UserVacation();
        u.setOperator(operator);
        InputStream inputStream = new BufferedInputStream(file.getInputStream());
        List<UserVacationImport2> list = ExcelUtil.ImportExcel(inputStream, UserVacationImport2.class);
        for(UserVacationImport2 userVacationImport : list){
            String userSerial = userVacationMapper.isExistByUserNo(userVacationImport.getUserNo());
            if(!StringUtils.isEmpty(userSerial)){

//                userVacationImport.setOperator(operator);
//                userVacationImport.setUserSerial(userSerial);
//                userVacationMapper.updateLock(userVacationImport);
//                userVacationMapper.addholidayInfo5(userVacationImport);
//                userVacationMapper.addholidayInfo8(userVacationImport);

                u.setUserSerial(userSerial);
                int faDing = userVacationMapper.getFaDing(u);
                if(Integer.valueOf(userVacationImport.getSdjqts())<0){
                    int sdjqts = userVacationMapper.getSdjqts(u);
                    if(userVacationImport.getSdjqts()*-1>sdjqts){
                        throw new ScmException(ExceptionEmun.USER_ERROR_FADING3);
                    }
                    faDing = userVacationImport.getSdjqts()+1;
                }
                if(faDing==0){
                    throw new ScmException(ExceptionEmun.USER_ERROR_FADING);
                }else if(userVacationImport.getSdjqts()>faDing){
                    throw new ScmException(ExceptionEmun.USER_ERROR_FADING2);
                }else{
                    u.setSxsj(DateUtils.getCurrentDate());
                    u.setJqsc(userVacationImport.getSdjqts());
                    u.setLx(3);
                    userVacationMapper.luRuVacation(u);
                    userVacationMapper.execTaskRuVacation();
                }

            }else{
                throw new ScmException(ExceptionEmun.USER_ERROR_NOTFOUND);
            }
        }
        return null;
    }

    @Override
    @Transactional
    public String upBreakExcel(MultipartFile file, HttpServletRequest request) throws Exception {
        String r = null;
        JSONObject user = (JSONObject) request.getSession().getAttribute("user");
        int operator = user.getInteger("userSerial");
        String currentDate = DateUtils.getCurrentDate();
        InputStream inputStream = new BufferedInputStream(file.getInputStream());
        List<UserVacationImport3> list = ExcelUtil.ImportExcel(inputStream, UserVacationImport3.class);
        for(UserVacationImport3 userVacationImport : list){
            String userSerial = userVacationMapper.isExistByUserNo(userVacationImport.getUserNo());
            if(!StringUtils.isEmpty(userSerial)){
                //判断调休不超过调休上限
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("userSerial",userSerial);
                jsonObject.put("minute",userVacationImport.getTxts());
                int ifRestrict = this.userVacationMapper.getBdNianjiaOne(jsonObject);
                if(0 == ifRestrict) {
                    userVacationImport.setOperator(operator);
                    userVacationImport.setUserSerial(userSerial);
//                userVacationMapper.updateBreak(userVacationImport);
                    userVacationMapper.addholidayInfo6(userVacationImport);
                    //重新计算假期
                    BdProcResetDataDto bdProcResetDataDto = new BdProcResetDataDto();
                    bdProcResetDataDto.setBeginDate(currentDate);
                    bdProcResetDataDto.setLx(1);
                    bdProcResetDataDto.setUserSerial(userSerial);
                    bdProcResetDataDto.setOperator(operator+"");
                    this.userVacationMapper.execResetData(bdProcResetDataDto);
                }else{
                    throw new ScmException(ExceptionEmun.USER_ERROR_FADING4);
                }
            }else{
                throw new ScmException(ExceptionEmun.USER_ERROR_NOTFOUND);
            }
        }
        return null;
    }

    @Override
    public void calculationVacation() {
        userVacationMapper.calculationVacation();
    }

    /**
     * 查询全局保存法定年假、福利年假清0时间设置
     * @return fdNjClearDate 法定年假清0时间 flNjCleaDate 福利年假清0时间
     */
    @Override
    public JSONObject getfdflNjClearDate() {
        return this.userVacationMapper.getfdflNjClearDate();
    }

    /**
     * 全局保存法定年假、福利年假清0时间设置
     * @param userVacation fdNjClearDate 法定年假清0时间 flNjCleaDate 福利年假清0时间
     * @return
     */
    @Override
    @Transactional
    public String updatefdflNjClearDate(UserVacation userVacation) {
        //返回信息字符串
        String r = null;
        int i = this.userVacationMapper.updatefdflNjClearDate(userVacation);
        if(i>0){

        }else{
            r = "全局法定年假、福利年假清0时间设置失败";
        }
        return r;
    }
}
